package com.wuyangnju.healthclub.service;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.wuyangnju.healthclub.dao.ActivityDao;
import com.wuyangnju.healthclub.entity.Activity;
import com.wuyangnju.healthclub.entity.Family;
import com.wuyangnju.healthclub.entity.Single;
import com.wuyangnju.healthclub.util.VirDate;

@Component
public class ActivityService {

	@PersistenceContext
	protected EntityManager em;

	@Resource
	private SingleService singleService;

	@Resource
	private FamilyService familyService;

	@Resource
	private ActivityDao activityDao;

	@Transactional
	public List<Activity> get() {
		return activityDao.retrieve(em);
	}

	@Transactional
	public boolean post(String cardId, String type, String description) {
		if ("single".equals(type)) {
			Single singlePerson = singleService.get(cardId);
			if (singlePerson.getMonthDelay() == 0) {
				Activity activity = new Activity(VirDate.get().getDate(),
						singlePerson, description);
				activityDao.create(em, activity);
				return true;
			} else {
				return false;
			}
		} else {
			Family family = familyService.get(cardId);
			if (family.getMonthDelay() == 0) {
				Activity activity = new Activity(VirDate.get().getDate(),
						family, description);
				activityDao.create(em, activity);
				return true;
			} else {
				return false;
			}
		}
	}
}